"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.logger = exports.LoggerLevel = exports.Logger = void 0;
var _debug = _interopRequireDefault(require("debug"));
/**
 * 日志级别
 */
let LoggerLevel = exports.LoggerLevel = /*#__PURE__*/function (LoggerLevel) {
  LoggerLevel[LoggerLevel["SUCCESS"] = 5] = "SUCCESS";
  LoggerLevel[LoggerLevel["LOG"] = 4] = "LOG";
  LoggerLevel[LoggerLevel["DEBUG"] = 3] = "DEBUG";
  LoggerLevel[LoggerLevel["WARN"] = 2] = "WARN";
  LoggerLevel[LoggerLevel["ERROR"] = 1] = "ERROR";
  return LoggerLevel;
}({});
/**
 * 日志打印
 */
class Logger {
  static level = LoggerLevel.LOG;
  static setLevel(level) {
    Logger.level = level;
  }

  /** 特定前缀，作为筛分的命名空间 */
  namespace = '';
  driver;
  constructor(namespace, driver) {
    this.namespace = namespace;
    this.driver = {
      success: (0, _debug.default)(namespace),
      log: (0, _debug.default)(namespace),
      debug: (0, _debug.default)(namespace),
      warn: (0, _debug.default)(namespace),
      error: (0, _debug.default)(namespace)
    };
    if (driver) this.setDriver(driver);
  }
  setDriver(driver) {
    this.driver = {
      ...this.driver,
      ...driver
    };
  }
  success(msg) {
    if (Logger.level < LoggerLevel.SUCCESS) return;
    if (this.driver.success === console.log) {
      console.log(`%c ${this.namespace}`, 'color: #52c41a; font-weight: bold', msg);
    } else {
      this.driver.success?.(msg);
    }
  }
  log(msg) {
    if (Logger.level < LoggerLevel.LOG) return;
    if (this.driver.log === console.log) {
      console.log(`%c ${this.namespace}`, 'color: #1677ff; font-weight: bold', msg);
    } else {
      this.driver.log?.(msg);
    }
  }
  debug(msg) {
    if (Logger.level < LoggerLevel.DEBUG) return;
    if (this.driver.debug === console.debug) {
      console.debug(`%c ${this.namespace}`, 'color: #faad14; font-weight: bold', msg);
    } else {
      this.driver.debug?.(msg);
    }
  }
  warn(msg) {
    if (Logger.level < LoggerLevel.WARN) return;
    if (this.driver.warn === console.warn) {
      console.warn(`%c ${this.namespace}`, 'color: #faad14; font-weight: bold', msg);
    } else {
      this.driver.warn?.(msg);
    }
  }
  error(msg) {
    if (Logger.level < LoggerLevel.ERROR) return;
    if (this.driver.error === console.error) {
      console.error(`%c ${this.namespace}`, 'color: #f5222d; font-weight: bold', msg);
    } else {
      this.driver.error?.(msg);
    }
  }
}

/**
 * 全局仅有一个实例
 */
exports.Logger = Logger;
const logger = exports.logger = new Logger('szzj');